<script lang="ts" setup>
import { ref, reactive, computed } from 'vue'
import { ElMessage, FormInstance, FormRules } from 'element-plus'
import { updateCustomer, createCustomer } from '@/api/customer'
import { getToken } from '@/utils/cache/cookies'
const dialogVisible = ref<boolean>(false)
const form = reactive({
  phone: '',
  name: '',
  id: '',
  role: ''
})
const emits = defineEmits(['refresh'])
const formRef = ref<FormInstance | null>(null)
const rules = reactive<FormRules>({
  phone: [
    {
      required: true,
      message: '请输入客服手机号',
      trigger: 'blur'
    }
  ],
  name: [
    {
      required: true,
      message: '请输入客服姓名',
      trigger: 'blur'
    }
  ],
  role: [
    {
      required: true,
      message: '请输入',
      trigger: 'blur'
    }
  ]
})
const reset = () => {
  formRef.value?.resetFields()
  form.id = ''
  dialogVisible.value = false
}
const handleClose = () => {
  formRef.value?.resetFields()
  form.id = ''
}
const submit = () => {
  formRef.value?.validate((valid) => {
    if (valid) {
      if (form.id) {
        update()
      } else {
        create()
      }
    }
  })
}
const create = () => {
  createCustomer({
    token: getToken() ?? '',
    ...form
  })
    .then((res) => {
      if (res.statusCode === '0') {
        dialogVisible.value = false
        ElMessage.success(res.data)
      } else {
        formRef.value?.resetFields()
      }
    })
    .catch((error) => {
      console.error(error)
    })
}
const update = () => {
  updateCustomer({
    token: getToken() ?? '',
    ...form
  })
    .then((res) => {
      if (res.statusCode === '0') {
        dialogVisible.value = false
        ElMessage.success(res.data)
        emits('refresh')
      } else {
        formRef.value?.resetFields()
      }
    })
    .catch((error) => {
      console.error(error)
    })
}
const open = () => {
  dialogVisible.value = true
}
const setData = (name: string, phone: string, role: string, id: string) => {
  form.name = name
  form.phone = phone
  form.role = role
  form.id = id
}
defineExpose({ open, setData })
const dialogTitle = computed(() => (form.id ? '修改客服' : '添加客服'))
</script>

<template>
  <el-dialog :title="dialogTitle" v-model="dialogVisible" width="500px" @close="handleClose">
    <el-form :model="form" ref="formRef" :rules="rules" label-width="100px">
      <el-form-item prop="name" label="姓名:">
        <el-input v-model="form.name" placeholder="请输入客服姓名" />
      </el-form-item>
      <el-form-item prop="phone" label="手机号:">
        <el-input v-model="form.phone" placeholder="请输入客服手机号" />
      </el-form-item>
      <el-form-item prop="role" label="角色">
        <el-select v-model="form.role" style="width: 100%">
          <el-option label="管理" value="admin" />
          <el-option label="客服" value="customer" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button plain @click="reset">取消</el-button>
      <el-button type="primary" @click="submit">发送</el-button>
    </template>
  </el-dialog>
</template>

<style scoped></style>
